package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.dao.BaseDao;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.CancelOut;
import cn.com.ege.mvc.model.business.Cutting;
import cn.com.ege.mvc.model.business.Material;
import cn.com.ege.mvc.model.business.OrderShoe;
import cn.com.ege.mvc.model.view.ProduceView;
import cn.com.ege.mvc.service.impl.business.CancelOutService;
import cn.com.ege.mvc.service.impl.business.CuttingService;
import cn.com.ege.mvc.service.impl.business.MaterialService;
import cn.com.ege.mvc.service.impl.business.OrderShoeService;
import cn.com.ege.mvc.service.impl.view.ProduceViewService;
import cn.com.ege.mvc.util.PageController;

public class CuttingAction extends BaseAction implements IAction, Serializable {

	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(CuttingAction.class);
	private PageController pager = null;
	private CuttingService cuttingService = null;
	private Cutting cutting = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		ProduceView produceView = new ProduceView();
		ProduceViewService produceViewService = new ProduceViewService();
		List<ProduceView> produceViewList = null;
		String act = request.getParameter("act");
		String order_shoe_id = request.getParameter("order_shoe_id");
		if (null != order_shoe_id && !"".equals(order_shoe_id)) {
			request.setAttribute("order_shoe_id", order_shoe_id);
			produceView.setId(Long.parseLong(order_shoe_id));
		}
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				Cutting cut = new Cutting();
				cut.setOrder_shoe_id(Long.parseLong(order_shoe_id));
				int totalRows = cuttingService.getRecordCount(cut);
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<Cutting> resourceList = cuttingService.list(new Cutting(),
						this.pager.getPageStartRow(), pager.getPageSize(),
						"and order_shoe_id=" + order_shoe_id
								+ " order by id desc", true);
				this.pager.setData(resourceList);
				produceViewList = produceViewService.listAll(produceView);
				request.setAttribute("produceViewList", produceViewList);

				Long total = 0L;
				for (int i = 33; i <= 40; i++) {
					total += BaseDao.getSum("size_" + i, "Cutting",
							"order_shoe_id=" + order_shoe_id + " and status=1");
				}
				
				//显示页面是完成冲单的数据
				CancelOut cancelOut = new CancelOut();
				CancelOutService cancelOutService = new CancelOutService();
				cancelOut.setOrder_shoe_id(produceView.getId());
				cancelOut = cancelOutService.get(cancelOut);
				Long[] canceled = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
				if(null != cancelOut){
					for (int i = 33; i <= 40; i++) {
						canceled[i-33] += BaseDao.getSum("size_" + i, "CancelOut",
								"shoe_id=" + cancelOut.getShoe_id());
					}
				}
				request.setAttribute("canceled", canceled);
				request.setAttribute("total", total);
				return getReturnURL(LIST);
			}
			// 点了添加或者点了修改
			else if ("get".equals(act)) {
				cutting = cuttingService.get(cutting);
				OrderShoe orderShoe = new OrderShoe();
				orderShoe.setId(Long.parseLong(order_shoe_id));
				OrderShoeService orderShoeService = new OrderShoeService();
				orderShoe = orderShoeService.get(orderShoe);
				request.setAttribute("orderShoe", orderShoe);
				Long[] size = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
				for (int i = 33; i <= 40; i++) {
					size[i - 33] = BaseDao.getSum("size_" + i, "Cutting",
							"order_shoe_id=" + order_shoe_id);
				}
				request.setAttribute("size", size);

				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					if (null != request.getParameter("status")) {
						cutting = cuttingService.get(cutting);
						cutting.setStatus(1L);
						OrderShoe orderShoe = new OrderShoe();
						orderShoe.setId(Long.parseLong(request
								.getParameter("order_shoe_id")));
						OrderShoeService orderShoeService = new OrderShoeService();
						orderShoe = orderShoeService.get(orderShoe);
						long sum0 = orderShoe.getSize_33()
								+ orderShoe.getSize_34()
								+ orderShoe.getSize_35()
								+ orderShoe.getSize_36()
								+ orderShoe.getSize_37()
								+ orderShoe.getSize_38()
								+ orderShoe.getSize_39()
								+ orderShoe.getSize_40();
						long sum1 = 0;
						for (int i = 33; i <= 40; i++) {
							sum1 += BaseDao.getSum("size_" + i, "Cutting",
									"order_shoe_id=" + order_shoe_id);
						}
						if (sum0 == sum1) {
							orderShoe.setTransact_status(1L);
							orderShoeService.save(orderShoe);
						}
						// 当状态改为 2 时，减库存
						if("2".equals(request.getParameter("status"))){
							cutting.setStatus(Long.valueOf(2));
							MaterialService materialService = new MaterialService();
							long total = cutting.getSize_33()
									+ cutting.getSize_34() + cutting.getSize_35()
									+ cutting.getSize_36() + cutting.getSize_37()
									+ cutting.getSize_38() + cutting.getSize_39()
									+ cutting.getSize_40();
							produceViewList = produceViewService.list(produceView);
							for (int i = 0; i < produceViewList.size(); i++) {
								Material material = new Material();
								material.setId(produceViewList.get(i)
										.getMaterial_iid());
								material = materialService.get(material);
								material.setRepertory(material.getRepertory()
										- produceViewList.get(i).getAmount()
										* total);
								materialService.update(material);
							}
						}
					}
					cuttingService.save(cutting);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("cutting", cutting);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					Cutting cut = new Cutting();
					if (null != id && !("".equals(id))) {
						cut.setId(Long.parseLong(id));
						cuttingService.delete(cut);
					}
				}
				return getReturnURL("getList");
			}
			// 打印生产流程单
			else if ("output".equals(act)) {
				cutting = cuttingService.get(cutting);
				request.setAttribute("cutting", cutting);
				long total = cutting.getSize_33() + cutting.getSize_34()
						+ cutting.getSize_35() + cutting.getSize_36()
						+ cutting.getSize_37() + cutting.getSize_38()
						+ cutting.getSize_39() + cutting.getSize_40();
				request.setAttribute("total", total);
				String[] material_name_list = { "", "", "", "", "", "", "", "",
						"", "" };
				produceViewList = produceViewService.listAll(produceView);
				for (Iterator<ProduceView> iterator = produceViewList
						.iterator(); iterator.hasNext();) {
					ProduceView pw = (ProduceView) iterator.next();
					material_name_list[pw.getType().intValue()] += pw
							.getMaterial_name()
							+ " "
							+ pw.getAmount()
							* total
							+ " "
							+ pw.getUnit()
							+ "<br/>";
				}
				request.setAttribute("material_name_list", material_name_list);
				produceView = produceViewService.get(produceView);
				request.setAttribute("produceView", produceView);

				request.setAttribute("w", getNum(produceView.getWaterproof()));
				request.setAttribute("i", getNum(produceView.getInnerheight()));
				request.setAttribute("h", getNum(produceView.getHeel()));
				return getReturnURL("output");
			}
		}
		addMessage("参数不正确!! act=" + act);
		return getReturnURL(ERROR);
	}

	private int[] getNum(String str) {
		String[] sqlit_str = str.split("/");
		int[] length = new int[3];
		for (int i = 0; i < sqlit_str.length; i++) {
			if(sqlit_str[i] != null){
				length[i] = sqlit_str[i].split(",").length;
			}
		}
		return length;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public CuttingService getCuttingService() {
		return cuttingService;
	}

	public void setCuttingService(CuttingService cuttingService) {
		this.cuttingService = cuttingService;
	}

	public Cutting getCutting() {
		return cutting;
	}

	public void setCutting(Cutting cutting) {
		this.cutting = cutting;
	}

}
